// Install an extra packages that we need
net install estout.pkg, from(http://fmwww.bc.edu/RePEc/bocode/e)
net install coefplot.pkg, from(http://fmwww.bc.edu/RePEc/bocode/c)

// Set some global variables to use for folder names.
global basedir "~/Dropbox/heyTim/UKKeynesians/UKKeynesiansReplication"
global texdir "$basedir/generated_tex"
global imgdir "$basedir/generated_images"
global datadir "$basedir/generated_data"

clear
use "$basedir/source_data/exp1_UniversityCollegeLondonresults_160318_Economy.dta"

gen     reduceDeficit = Q1
replace reduceDeficit = Q3 if reduceDeficit == 9
replace reduceDeficit = .  if reduceDeficit == 9

gen     taxspend = Q2
replace taxspend = Q4 if taxspend == 99
replace taxspend = .  if taxspend == 99

gen     treatment = 1 if Q1 < 9
replace treatment = 2 if Q2 < 99
replace treatment = 3 if Q3 < 9
replace treatment = 4 if Q4 < 99

gen     goodMacro = 1 if inlist(treatment, 1, 2)
replace goodMacro = 0 if goodMacro == .

label define Lab_GoodMacro 0 "$ Econ_i $: Bad" 1 "$ Econ_i $: Good"
label values goodMacro Lab_GoodMacro


gen wave = 1 if endtime < date("2016/03/10", "YMD")
replace wave = 2 if wave == .


//// Seems like an error in the data on this one!
drop if treatment == 4 & reduceDeficit != .

rename profile4 profile_gross_household
rename pastvote pastvote_2015
rename profile9 profile_newspaper_readership
rename partyid_ partyid_2015


gen     grossHHInc = 2.5 if profile_gross_household == 1
replace grossHHInc = 7.5 if profile_gross_household == 2
replace grossHHInc = 12.5 if profile_gross_household == 3
replace grossHHInc = 17.5 if profile_gross_household == 4
replace grossHHInc = 22.5 if profile_gross_household == 5
replace grossHHInc = 27.5 if profile_gross_household == 6
replace grossHHInc = 32.5 if profile_gross_household == 7
replace grossHHInc = 37.5 if profile_gross_household == 8
replace grossHHInc = 42.5 if profile_gross_household == 9
replace grossHHInc = 47.5 if profile_gross_household == 10
replace grossHHInc = 55 if profile_gross_household == 11
replace grossHHInc = 65 if profile_gross_household == 12
replace grossHHInc = 85 if profile_gross_household == 13
replace grossHHInc = 125 if profile_gross_household == 14
replace grossHHInc = 150 if profile_gross_household == 15
replace grossHHInc = 0.1 if profile_gross_household > 15

gen     logGrossHHInc = log(grossHHInc)

gen     grossHHIncdk = 1 if profile_gross_household > 15
replace grossHHIncdk = 0 if profile_gross_household <= 15


label define Lab_GrossHH 1 "$ HHInc_i $: <5k" 2 "$ HHInc_i $: 5-10k" 3 "$ HHInc_i $: 10-15k" 4 "$ HHInc_i $: 15-20k" 5 "$ HHInc_i $: 20-25k" 6 "$ HHInc_i $: 25-30k" 7 "$ HHInc_i $: 30-35k" 8 "$ HHInc_i $: 35-40k" 9 "$ HHInc_i $: 40-45k" 10 "$ HHInc_i $: 45-50k" 11 "$ HHInc_i $: 50-60k" 12 "$ HHInc_i $: 60-70k" 13 "$ HHInc_i $: 70-100k" 14 "$ HHInc_i $: 100-150k" 15 "$ HHInc_i $: >150k" 16 "$ HHInc_i $: DK" 17 "$ HHInc_i $: N/R" 98 "$ HHInc_i $: N/A"
label values profile_gross_household Lab_GrossHH


recode partyid_2015 (1 3=1) (2 4 5 7 = 2) (6 8 9 = 3) (10 99 = 4), gen(pidgroup)

label define Lab_PIdGroup 1 "$ PId_i $: C/LD" 2 "$ PId_i $: Left" 3 "$ PId_i $: Other" 4 "$ PId_i $: NA"
label values pidgroup Lab_PIdGroup


label define Lab_Vote 1 "$ Voted_i $: Con" 2 "$ Voted_i $: Lab" 3 "$ Voted_i $: LD" 4 "$ Voted_i $: SNP" 5 "$ Voted_i $: PC" 6 "$ Voted_i $: UKIP" 7 "$ Voted_i $: Green" 8 "$ Voted_i $: BNP" 9 "$ Voted_i $: Other" 99 "$ Voted_i $: DK" 999 "$ Voted_i $: N/A"
label values pastvote_2015 Lab_Vote

label define Lab_Newspaper 1 "$ Newspaper:$ Express" 2 "$ Newspaper:$ Mail" 3 "$ Newspaper:$ Mirror" 4 "$ Newspaper:$ Star" 5 "$ Newspaper:$ Sun" 6 "$ Newspaper:$ Telegraph" 7 "$ Newspaper:$ FT" 8 "$ Newspaper:$ Guardian" 9 "$ Newspaper:$ Independent" 10 "$ Newspaper:$ Times" 11 "$ Newspaper:$ Scotsman" 12 "$ Newspaper:$ Herald" 13 "$ Newspaper:$ Western Mail" 14 "$ Newspaper:$ Other local" 15 "$ Newspaper:$ Other" 16 "$ Newspaper:$ None"
label values profile_newspaper_readership Lab_Newspaper

*profile0        double  %10.0g     profile0   Age finished education
*profile6        double  %10.0g     profile6   Household size - number of children
*profile9        double  %10.0g     profile9   Newspaper readership - main daily
*profil10        double  %10.0g     profil10   House Tenure


reg reduceDeficit i.goodMacro [pweight=W8]
local exp1_N = e(N)
file open  afile using "$texdir/exp1_N.tex", write replace
file write afile "`exp1_N'"
file close afile
estimates store Exp1BaseOLS
reg reduceDeficit i.goodMacro i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household [pweight=W8]
estimates store Exp1ContOLS



ologit reduceDeficit i.goodMacro [pweight=W8]
estimates store Exp1BaseOLo
ologit reduceDeficit i.goodMacro i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household [pweight=W8]
estimates store Exp1ContOLo

reg reduceDeficit i.goodMacro##i.pidgroup [pweight=W8]
estimates store Exp1PIdBaseOLS
reg reduceDeficit i.goodMacro##i.pidgroup i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household [pweight=W8]
estimates store Exp1PIdContOLS

ologit reduceDeficit i.goodMacro##i.pidgroup [pweight=W8]
estimates store Exp1PIdBaseOLo
ologit reduceDeficit i.goodMacro##i.pidgroup i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household [pweight=W8]
estimates store Exp1PIdContOLo



global estout_options label replace stats(N r2 r2_p ll, labels("$ N $" "$ R^2 $" "$ Pseudo R^2 $" "Log Lik.")) cells( " b(fmt(a2)) se(fmt(2))" ) starlevels(* 0.10 ** 0.05 *** 0.01) style(tex) posthead("\hline") prefoot(\hline) postfoot("\hline") mlabels("(1)" "(2)" "(3)" "(4)" "(5)" "(6)") drop(0.goodMacro#1.pidgroup 0.goodMacro#2.pidgroup 0.goodMacro#3.pidgroup 0.goodMacro#4.pidgroup)
global estout_ologit_options varlabels(cut1:_cons "Cut 1" cut2:_cons "Cut 2" cut3:_cons "Cut 3" cut4:_cons "Cut 4" _cons "Constant") eqlabels("", none)

estout  Exp1BaseOLS		Exp1BaseOLo		Exp1PIdBaseOLS		Exp1PIdBaseOLo		using "$texdir/exp1.tex",			$estout_options $estout_ologit_options
estout  Exp1ContOLS		Exp1ContOLo		Exp1PIdContOLS		Exp1PIdContOLo		using "$texdir/exp1_controls.tex",	$estout_options $estout_ologit_options


label define Lab_VotePlain 1 "Voted: Con" 2 "Voted: Lab" 3 "Voted: LD" 4 "Voted: SNP" 5 "Voted: PC" 6 "Voted: UKIP" 7 "Voted: Green" 8 "Voted: BNP" 9 "Voted: Other" 99 "Voted: DK" 999 "Voted: N/A"
label values pastvote_2015 Lab_VotePlain


label define Lab_NewspaperPlain 1 "Newspaper: Express" 2 "Newspaper: Mail" 3 "Newspaper: Mirror" 4 "Newspaper: Star" 5 "Newspaper: Sun" 6 "Newspaper: Telegraph" 7 "Newspaper: FT" 8 "Newspaper: Guardian" 9 "Newspaper: Independent" 10 "Newspaper: Times" 11 "Newspaper: Scotsman" 12 "Newspaper: Herald" 13 "Newspaper: Western Mail" 14 "Newspaper: Other local" 15 "Newspaper: Other" 16 "Newspaper: None"
label values profile_newspaper_readership Lab_NewspaperPlain


local keepers  1.goodMacro
local controlkeepers 1.pastvote_2015 2.pastvote_2015 3.pastvote_2015 16.profile_newspaper_readership 6.profile_newspaper_readership 8.profile_newspaper_readership
local renamers 1.goodMacro = "Treat: Good Econ"
coefplot (Exp1BaseOLS, label(Base) keep(`keepers') rename(`renamers'))  (Exp1ContOLS, label(+Controls) keep(`keepers' `controlkeepers') rename(`renamers')), xline(0) scheme(s1mono) cismooth levels(95) baselevels
graph export "$imgdir/exp1_coefplot.pdf", replace

local keepers  1.goodMacro 1.goodMacro#2.pidgroup 1.goodMacro#3.pidgroup 1.goodMacro#4.pidgroup
local renamers 1.goodMacro = "Party Id: Con/LD" 1.goodMacro#2.pidgroup = "Party Id: Lab/SNP/PC/Green" 1.goodMacro#3.pidgroup = "Party Id: UKIP/BNP/Other" 1.goodMacro#4.pidgroup = "Party Id: None"
coefplot (Exp1PIdBaseOLS, label(Base) keep(`keepers') rename(`renamers')) (Exp1PIdContOLS, label(With controls) keep(`keepers') rename(`renamers')), xline(0) scheme(s1mono) cismooth levels(95)
graph export "$imgdir/exp1_pid_coefplot.pdf", replace


keep reduceDeficit goodMacro pastvote_2015 partyid_2015 profile_newspaper_readership profile_gross_household pidgroup W8
save "$datadir/exp1.dta", replace
